﻿using AutoMapper;
using ManagementApi.Authorizers;
using ManagementApi.Models;
using ManagementApi.Models.Device;
using ManagementApi.Mvc.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules;
using Modules.Device;
using PmSoft.ApplicationContext;
using PmSoft.Utilities;

namespace ManagementApi.Controllers.AdminPannel
{
    /// <summary>
    /// App考勤授权
    /// </summary>
    [Route("/api/v1/sysuser/AppCheckinAuth")]
    [ApiController]
    [Authorize(Roles = "SysUser")]
    public class AppCheckinAuthController : ControllerBase
    {
        private readonly AppCheckinAuthService appCheckinAuthService;
        private readonly IMapper mapper;
        private readonly IApplicationContext applicationContext;
        private readonly ProjectOwnAuthorizer ownAuthorizer;

        public AppCheckinAuthController(AppCheckinAuthService appCheckinAuthService, IMapper mapper, IApplicationContext applicationContext, ProjectOwnAuthorizer ownAuthorizer)
        {
            this.appCheckinAuthService = appCheckinAuthService;
            this.mapper = mapper;
            this.applicationContext = applicationContext;
            this.ownAuthorizer = ownAuthorizer;
        }

        /// <summary>
        /// App考勤授权分页
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("page")]
        public ApiResult<PageDto<AppCheckinAuthDto>> GetCorpInfos([FromQuery] AppCheckinAuthQuery query, int pageNum = 1, int pageSize = 20)
        {
            var data = appCheckinAuthService.GetAppCheckinAuths(query, pageNum, pageSize);
            var dtos = data.AsPageDto(source => { return source.AsDto(applicationContext); });
            return ApiResult<PageDto<AppCheckinAuthDto>>.Success(dtos);
        }

        /// <summary>
        /// 创建App考勤授权信息
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost("create")]
        [PermissionCheck("sys:appcheckinauth:add")]
        public ApiResult Add([FromBody] AppCheckinAuthEditForm form)
        {
            AppCheckinAuth appCheckinAuth = new();
            mapper.Map(form, appCheckinAuth);
            appCheckinAuth.CreateTime = DateTime.Now;
            appCheckinAuth.LastUpdateTime = DateTime.Now;
            appCheckinAuth.IsDisable = false;
            appCheckinAuthService.Create(appCheckinAuth);
            return ApiResult.Success("创建成功");
        }

        /// <summary>
        /// 编辑App考勤授权信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        [PermissionCheck("sys:appcheckinauth:edit")]
        public ApiResult Edit(int id, [FromBody] AppCheckinAuthEditForm form)
        {
            AppCheckinAuth? appCheckinAuth = appCheckinAuthService.Get(id);
            if (appCheckinAuth == null) return ApiResult.Failure("授权信息不存在");

            #region 验证数据权限

            ownAuthorizer.CheckAndThrowIfOwnAuthenticationFailed(appCheckinAuth.ProjectId);

            #endregion 验证数据权限

            mapper.Map(form, appCheckinAuth);
            appCheckinAuth.LastUpdateTime = DateTime.Now;
            appCheckinAuthService.Update(appCheckinAuth);
            return ApiResult.Success("修改成功");
        }

        /// <summary>
        /// app考勤授权表单详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}/form")]
        [PermissionCheck("sys:appcheckinauth:details")]
        public ApiResult<AppCheckinAuthEditForm> Get(int id)
        {
            AppCheckinAuth? appCheckinAuth = appCheckinAuthService.Get(id);
            if (appCheckinAuth == null) return ApiResult<AppCheckinAuthEditForm>.Failure("授权信息不存在");
            return ApiResult<AppCheckinAuthEditForm>.Success(appCheckinAuth.AsForm());
        }

        /// <summary>
        /// 删除app考勤授权信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("{ids}")]
        [PermissionCheck("sys:appcheckinauth:delete")]
        public ApiResult Delete(string ids)
        {
            var data = appCheckinAuthService.GetAppCheckinAuths(ValueUtility.ConvertToList<int>(ids));

            #region 验证数据权限

            ownAuthorizer.CheckAndThrowIfOwnAuthenticationFailed(data.Select(i => i.ProjectId));

            #endregion 验证数据权限

            appCheckinAuthService.Deleted(ValueUtility.ConvertToList<int>(ids));
            return ApiResult.Success("删除成功");
        }

        /// <summary>
        /// 禁用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut("disable/{id}")]
        [PermissionCheck("sys:appcheckinauth:disable")]
        public ApiResult DisableDevice(int id)
        {
            AppCheckinAuth? appCheckinAuth = appCheckinAuthService.Get(id);
            if (appCheckinAuth == null) return ApiResult.Failure("授权信息不存在");

            #region 验证数据权限

            ownAuthorizer.CheckAndThrowIfOwnAuthenticationFailed(appCheckinAuth.ProjectId);

            #endregion 验证数据权限

            appCheckinAuth.IsDisable = true;
            appCheckinAuth.LastUpdateTime = DateTime.Now;
            appCheckinAuthService.Update(appCheckinAuth);
            return ApiResult.Success("禁用成功");
        }

        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut("enable/{id}")]
        [PermissionCheck("sys:appcheckinauth:enable")]
        public ApiResult EnableDevice(int id)
        {
            AppCheckinAuth? appCheckinAuth = appCheckinAuthService.Get(id);
            if (appCheckinAuth == null) return ApiResult.Failure("授权信息不存在");

            #region 验证数据权限

            ownAuthorizer.CheckAndThrowIfOwnAuthenticationFailed(appCheckinAuth.ProjectId);

            #endregion 验证数据权限

            appCheckinAuth.IsDisable = false;
            appCheckinAuth.LastUpdateTime = DateTime.Now;
            appCheckinAuthService.Update(appCheckinAuth);
            return ApiResult.Success("启用成功");
        }
    }
}